package org.apache.spark

import org.apache.spark.rpc.{RpcCallContext, RpcEndpoint, RpcEnv}

class HelloEndPoint(override val rpcEnv:RpcEnv) extends RpcEndpoint{

  override def onStart(): Unit = {
    println(rpcEnv.address)
    println("start HelloEndPoint")
  }

  //不需要返回的服务方法
  override def receive: PartialFunction[Any, Unit] = {
    case SayHi(msg)=>{
      println(s"receive msg: $msg")
    }
  }

  /**
   * 接收消息，并回复消息
   * @param context
   * @return
   */
  override def receiveAndReply(context: RpcCallContext): PartialFunction[Any, Unit] = {
    case SayHi(msg) => {
      println(s"receive msg, $msg")
      context.reply(s"I'am server, $msg")
    }
    case SayBye(msg) => {
      println(s"receive msg, $msg")
      context.reply(s"I'am server, $msg")
    }
  }



}

case class SayHi(msg:String);

case class SayBye(msg:String)